

require('./common');

exports.load = function(req, res){
	var id = req.params.id;
	var tblname = req.params.tblname;
	var formName = req.params.formName;
	var rCount=0;
	
	var strSQL = "SELECT ws.ID as ws_id, ws.FormName as ws_formName, "+
		"wf.id as wf_id, wf.title as wf_title, wf.form_id as form_id "+
		"FROM tbworkflow wf LEFT JOIN tblworkspace ws on wf.form_id = ws.ID";
	
	conn.query(strSQL, function(err, results, fields){
		if(err) throw err;
		var output = '<html><head></head><body  style="background-color:#fafafa"><h4>Home / Settings /</h4><h2><img src="/images/ui/workflow.png" style="vertical-align:middle" style="border:0px">&nbsp;Workflow</h2>'+
				'<table cellpadding=0 cellspacing=0 width="98%">';
		output += '<tr style="background-color:#f4f4f4">';
		output += '<td colspan=2><a href="#" id="anewWF" class="bluebutton">New Workflow</a></td>';
		output += '<td align="right"><input type="text" value="Search..." style="width:200px;font-style:Italic;color:#777" /></td>';
		output += '</tr>';
		output += '<tr>';
		output += '<td colspan=3><hr></td>';
		output += '</tr>';
		output += '<tr>';
		output += '<td colspan=3>';
		for (var index in results) {
			output +=  '<div class="requestview">';
			output += '<div class="requestBox">';
			output += '<h4 style="display: inline;"><img src="/images/ui/workflow.png" style="vertical-align:middle"/>&nbsp;'+ results[index].wf_title +'</h4>';
			output += '<br/><br/>'
			output += '<font color="#777" style="font-weight:normal"> ' +  results[index].ws_formName + '</font>';
			output += '<div style="float:right"><a href="/workflow/' +  results[index].wf_id + '/' +  results[index].form_id + '"><img src="/images/ui/edit.png" style="vertical-align:middle"/></a>&nbsp;';
			output += '<a href="#"><img src="/images/ui/delete.png" style="vertical-align:middle"/></a></div>';
			output += '</div><br style="clear:both;"/>';
			output += '</div><br/>';
			rCount++;
		}
		
		if(rCount==0){
			output += '<tr style="background-color:#f2f0bf">';
			output +=  '<td colspan=3 style="border:1px Solid #808a10;padding:10px" valign="top">No record found.</td>';
			output +=  '</tr>';
		}
		output += '</td>';
		output += '</tr>';
		output += '</table>';
		output += '</body></html>';
		
		
		res.writeHead(200, {'Content-Type': 'text/html'});
		res.end(output);
	});
}